package com.design.twords.adatper;

import android.content.Context;
import android.content.Intent;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.design.twords.R;
import com.design.twords.activity.WordDetailActivity;
import com.design.twords.config.ConstantValue;
import com.design.twords.entity.ItemWordList;
import com.design.twords.utils.MediaHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Create by Alen
 * Time:  23:56
 */
public class  WordsListAdapter extends RecyclerView.Adapter<WordsListAdapter.MyViewHolder> {
    private static final String  TAG = "WordsListAdapter";

    public interface onItemClickListener{
        void itemLongClickListener(MyViewHolder myViewHolder);
    }

    private LayoutInflater myInflater;
    private Context context;
    private List<ItemWordList> wordslist;
    private int viewId;
    public boolean selectModel = false;

    public SparseBooleanArray checkFlags = new SparseBooleanArray();
    private onItemClickListener listener;
    public Map<Integer, Boolean> map = new HashMap<>();
    public List<ItemWordList> check_words = new ArrayList<>();

    public WordsListAdapter(Context context, List<ItemWordList> wordslist, int viewId) {
        this.myInflater = LayoutInflater.from(context);
        this.context = context;
        this.wordslist = wordslist;
        this.viewId = viewId;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        MyViewHolder myViewHolder = new MyViewHolder(myInflater.inflate(viewId, parent, false));
        if (selectModel)
            myViewHolder.wordlist_checkBox.setVisibility(View.VISIBLE);
        else
            myViewHolder.wordlist_checkBox.setVisibility(View.INVISIBLE);

        myViewHolder.wordlist_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int position = myViewHolder.getAdapterPosition();
                if (isChecked)
                    map.put(position,true);
                else
                    map.remove(position);

            }
        });
        return myViewHolder;
    }


    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        ItemWordList item = wordslist.get(position);
        holder.wordslist_wordname.setText(item.getWord());
        holder.wordlist_wordtrans.setText(item.getTrans());

        if (selectModel)
            holder.wordlist_checkBox.setVisibility(View.VISIBLE);
        else
            holder.wordlist_checkBox.setVisibility(View.INVISIBLE);

        if (map != null && map.containsKey(position)) {
            holder.wordlist_checkBox.setChecked(true);
        } else{
            holder.wordlist_checkBox.setChecked(false);
        }

        holder.wordlist_song.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = (String) holder.wordslist_wordname.getText();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        MediaHelper.play(text);
                    }
                }).start();


            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectModel) {
                    holder.wordlist_checkBox.setVisibility(View.VISIBLE);
                    if (holder.wordlist_checkBox.isChecked()) {
                        holder.wordlist_checkBox.setChecked(false);
                        check_words.remove(item);

                    } else {
                        holder.wordlist_checkBox.setChecked(true);
                        check_words.add(item);
                    }
                    return;
                }

                //跳转到详情界面
                Intent intent = new Intent(context, WordDetailActivity.class);
                if (item.getWordBook().equals("ocr"))
                    intent.putExtra("flags", ConstantValue.SEARCH_WORD);
                else
                    intent.putExtra("flags", ConstantValue.LOOK_WORD);
                intent.putExtra("wordId", item.getWordId()+"");
                intent.putExtra("booktype", item.getWordBook());
                context.startActivity(intent);

            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (!selectModel){
                    listener.itemLongClickListener(holder);
                    selectModel =true;
                    holder.wordlist_checkBox.setVisibility(View.VISIBLE);
                    holder.wordlist_checkBox.setChecked(true);
                    check_words.add(item);
                }
                return true;
            }
        });


    }


    @Override
    public int getItemCount() {
        return wordslist.size();
    }


    public void setWordslist(List<ItemWordList> wordslist) {
        this.wordslist = wordslist;
    }

    public void setListener(onItemClickListener listener) {
        this.listener = listener;
    }

    public List<ItemWordList> getCheck_words() {
        return check_words;
    }

    public boolean isSelectModel() {
        return selectModel;
    }

    public void setSelectModel(boolean selectModel) {
        this.selectModel = selectModel;
    }

    public void clearMap(){
        map.clear();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView wordslist_wordname, wordlist_wordtrans;
        private ImageView wordlist_song;
        private CheckBox wordlist_checkBox;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            wordslist_wordname = itemView.findViewById(R.id.wordslist_wordname);
            wordlist_wordtrans = itemView.findViewById(R.id.wordlist_wordtrans);
            wordlist_song = itemView.findViewById(R.id.wordlist_song);
            wordlist_checkBox = itemView.findViewById(R.id.worddetail_checkbox);
        }
    }
}
